<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.activityName"
            placeholder="活动的名称"
            clearable
          />
          <el-input
            v-model="searchModel.activityStatus"
            placeholder="(0:未进行 1:进行中 2:已过期)"
            clearable
          />
          <el-button
            type="primary"
            round
            icon="el-icon-search"
            @click="getReleaseActivityList"
          >查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            type="primary"
            icon="el-icon-plus"
            circle
            @click="openEditUI(null)"
          />
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="releaseActivityList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="用户ID" width="100" />
        <el-table-column prop="activityName" label="活动名称" width="120" />
        <el-table-column prop="activityIntroduce" label="活动介绍" width="180" />
        <el-table-column prop="activityStatus" label="活动状态" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.activityStatus == 1 ">进行中</el-tag>
            <el-tag v-else-if="scope.row.activityStatus == 2" type="danger">已过期</el-tag>
            <el-tag v-else-if="scope.row.activityStatus == 0" >未开启</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="activityAddress" label="活动地点" width="120" />
        <el-table-column prop="activityStartTime" label="活动开始时间" width="180" />
        <el-table-column prop="activityEndTime" label="活动结束时间" width="180" />
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="openEditUI(scope.row.id)" />
            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="deleteActivity(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 用户信息编辑对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="clearForm"
    >
      <el-form ref="activityFormRef" :model="activityForm" :rules="rules">
        <el-form-item
          label="活动名称"
          prop="activityName"
          :label-width="formLabelWidth"
        >
          <el-input v-model="activityForm.activityName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="活动介绍" :label-width="formLabelWidth">
          <el-input v-model="activityForm.activityIntroduce" autocomplete="off" />
        </el-form-item>
        <el-form-item label="活动地点" :label-width="formLabelWidth">
          <el-input v-model="activityForm.activityAddress" autocomplete="off" />
        </el-form-item>
        <el-form-item label="活动开启时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="activityForm.activityStartTime"
            type="date"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          />
        </el-form-item>

        <el-form-item label="活动结束时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="activityForm.activityEndTime"
            type="date"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveActivity">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import releaseActivityApi from '@/api/releaseActivity'
export default {
  data() {
    return {
      formLabelWidth: '100px',
      activityForm: {
        activityIntroduce: '',
        activityEndTime: '',
        activityStartTime: ''
      },
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
        activityStatus: ''
      },
      releaseActivityList: [],
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 50,
            message: '长度在 3 到 50 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getReleaseActivityList()
  },
  methods: {
    deleteActivity(activity) {
      this.$confirm(`您确认删除活动 ${activity.activityName} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        releaseActivityApi.deleteActivityById(activity.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.getReleaseActivityList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveActivity() {
      // 触发表单验证
      this.$refs.activityFormRef.validate((valid) => {
        if (valid) {
          // 提交请求给后台
          releaseActivityApi.saveActivity(this.activityForm).then(response => {
            // 成功提示
            this.$message({
              message: response.message,
              type: 'success'
            })
            // 关闭对话框
            this.dialogFormVisible = false
            // 刷新表格
            this.getReleaseActivityList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.$refs.activityFormRef.clearValidate()
    },
    openEditUI(id) {
      if (id == null) {
        this.title = '新增活动'
      } else {
        this.title = '修改活动'
        // 根据id查询用户数据
        releaseActivityApi.getActivityById(id).then(response => {
          this.activityForm = response.data
        })
      }
      this.dialogFormVisible = true
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getReleaseActivityList()
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo
      this.getReleaseActivityList()
    },
    getReleaseActivityList() {
      releaseActivityApi.getActivityList(this.searchModel).then((response) => {
        this.releaseActivityList = response.data.rows
        this.total = response.data.total
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 85%;
}
</style>
